package com.spoplar.queue;
import java.io.Console;

import javax.jms.BytesMessage;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;


public class JMSConsumer {
	public static final String USERNAME =ActiveMQConnection.DEFAULT_USER ;  //用户名
	public static final String PASSWORD =ActiveMQConnection.DEFAULT_PASSWORD ; //密码
	public static final String BROKER_URL =ActiveMQConnection.DEFAULT_BROKER_URL ; //消息总线

	public static final int SENDNUMS = 10;
	public static void main(String[] args) throws JMSException {
		
		ConnectionFactory connectionFactory;
		Connection connection;
		Session session;
		Destination destination;
		MessageConsumer messageConsumer;
		
		connectionFactory = new ActiveMQConnectionFactory(
				JMSConsumer.USERNAME, 
				JMSConsumer.PASSWORD, 
				JMSConsumer.BROKER_URL);
		connection = connectionFactory.createConnection();
		connection.start();
		session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
		destination=session.createQueue("Queue 1");
		messageConsumer=session.createConsumer(destination);
		Message message = messageConsumer.receive();
		if(message instanceof TextMessage){
			System.out.println("TextMessage");
			System.out.println(((TextMessage)message).getText());
		}
		
		if(message instanceof BytesMessage){
			System.out.println("BytesMessage");
			 	byte[] b = new byte[1024];
	            int len = -1;
	            BytesMessage bm = (BytesMessage) message;
	            while ((len = bm.readBytes(b)) != -1) {
	                System.out.println(new String(b, 0, len));
	            }
		}
		if(message instanceof ObjectMessage){
			//System.out.println(((TextMessage)message).getText());
			System.out.println("objectMessage");
		}
		
		//String string = new String(bytes);
		//System.out.println(string);
		
//		while(true){
//			ObjectMessage textMessage=(ObjectMessage)messageConsumer.receive(1000000);
//			
//			if(textMessage!=null){
//				System.out.println("收到的消息："+textMessage.toString());
//			}else{
//				break;
//			}
//		}
	}
	
}
